<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ taglib prefix="c"  uri="http://java.sun.com/jsp/jstl/core" %>
<script type="text/javascript" src="./js/jquery-1.10.2.js"></script>
<script type="text/javascript">

	var save_gubun=0; // 신규입력인지 수정인지 구분 : 0: 신규/ 1: 수정
	var oldhakbun = ''; // 수정되기 전의 학번 임시 저장
	
	function init(){
		hakbun.value='';
		name1.value='';
		address.value='';
		passwd.value='';
		email.value='';
		tel.value='';
		jumin1.value='';
		jumin2.value='';
		
		$('input:radio[name=gender]:input[value=M]').prop('checked',true);
	}

	function save(){
		var hakbun = $('#hakbun').val();
		var name = $('#name1').val();
		var address = $('#address').val();
		var gwa_code = $('#gwa_code').val();
		var passwd = $('#passwd').val();
		var email = $('#email').val();
		var tel = $('#tel').val();
		var jumin1 = $('#jumin1').val();
		var jumin2 = $('#jumin2').val();
		var gender = $("input[name='gender']:checked").val();
		var data={"hakbun":hakbun, "name":name, "address":address, "gwa_code":gwa_code,"passwd":passwd,
				"email":email,"tel":tel,"jumin1":jumin1,"jumin2":jumin2,"gender":gender};

		if(hakbun ==''){
			alert('학번을 입력하세요');
			return;
		}
		if(name == ''){
			alert('이름을 입력하세요');
			return;
		}
		if(address==''){
			alert('주소를 입력하세요');
			return;
		}
		if(passwd==''){
			alert('비밀번호를 입력하세요');
			return;
		}
		if(email==''){
			alert('이메일을 입력하세요');
			return;
		}
		if(tel==''){
			alert('전화번호를 입력하세요');
			return;
		}
		if(jumin1==''){
			alert('주민번호 앞자리를 입력하세요');
			return;
		}
		if(jumin2==''){
			alert('주민번호 뒷자리를 입력하세요');
			return;
		}
		
		if(save_gubun!='1')
		{
			$.ajax({
				type:"post",
				data:data,
				url:"checkId.do",
				dataType:"json",
				success:function(data1){
					
					if(data1.result=='exists')
					{
						alert('해당 학번이 이미 존재합니다.');
					}else
					{
						$("#output").empty();
						var head = "<tr><td></td><td>학번</td><td>성명</td><td>주소</td>"+
								   "<td>전공</td><td>비밀번호</td><td>이메일</td><td>전화</td>"+
								   "<td>주민번호</td><td>성별</td></tr>";
		 					
						$("#output").append(head);

						$(data1).each(function(index,item){
							output+="<tr>";
							output+='<td><input type="button" value="수정" onclick="update(\''+item.hakbun+'\',\''+item.name+'\',\''+item.address+'\',\''+item.passwd+'\',\''+item.email+'\',\''+item.tel+'\',\''+item.jumin1+'\',\''+item.jumin2+'\',\''+item.gender+'\')"></td>';
							output+="<td><input type=checkbox name=s_c_hakbun value="+item.hakbun+">"+ item.hakbun + "</td>";
							output+="<td>" + item.name + "</td>";
							output+="<td>" + item.address + "</td>";
							output+="<td>" + item.gwa_code + "</td>";
							output+="<td>" + item.passwd + "</td>";
							output+="<td>" + item.email + "</td>";
							output+="<td>" + item.tel + "</td>";
							output+="<td>" + item.jumin1 +"-"+item.jumin2 + "</td>";
								
							output+="<td>" + item.gender + "</td>";
							output+="</tr>";
							
							$("#output").append(output);
							output = '';
						});
					}
				},
				error:function(e){
				}
			});
			
			init(); /* 입력칸 초기화 */
		}else
		{
			var data={"hakbun":hakbun, "name":name, "address":address, "gwa_code":gwa_code,"passwd":passwd,
					"email":email,"tel":tel,"jumin1":jumin1,"jumin2":jumin2,"gender":gender,"oldhakbun":oldhakbun};
			
			// 해당 자료를 update 해야 한다. 
			$.ajax({
				type:"post",
				data:data,
				url:"updateStudent.do",
				dataType:"json",
				success:function(data2)
				{
					$("#output").empty();
					var head = "<tr><td></td><td>학번</td><td>성명</td><td>주소</td>"+
							   "<td>전공</td><td>비밀번호</td><td>이메일</td><td>전화</td>"+
							   "<td>주민번호</td><td>성별</td></tr>";
	 					
					$("#output").append(head);
					/* $('#listTable').html(''); */
					
					$(data2).each(function(index,item){
						output+="<tr>";
						/* 수정후 화면 갱신 */
						output+='<td><input type="button" value="수정" onclick="update(\''+item.hakbun+'\',\''+item.name+'\',\''+item.address+'\',\''+item.passwd+'\',\''+item.email+'\',\''+item.tel+'\',\''+item.jumin1+'\',\''+item.jumin2+'\',\''+item.gender+'\')"></td>';
						output+="<td><input type=checkbox name=s_c_hakbun value="+item.hakbun+">"+item.hakbun+"</td>";
						output+="<td>" + item.name + "</td>";
						output+="<td>" + item.address + "</td>";
						output+="<td>" + item.gwa_code + "</td>";
						output+="<td>" + item.passwd + "</td>";
						output+="<td>" + item.email + "</td>";
						output+="<td>" + item.tel + "</td>";
						output+="<td>" + item.jumin1 +"-"+item.jumin2 + "</td>";
							
						output+="<td>" + item.gender + "</td>";
						output+="</tr>";
						
						$("#output").append(output);
						output = '';
					});
				}
			});
			save_gubun = 0; //업데이트 후 다시 save_gubun = 0으로 셋팅
			init(); // 입력칸 초기화
		}
	}		
	
	function del()
	{
		
		var s_c_hakbun = $('input[name=s_c_hakbun]:checked');
		
		var arr=""; 
		for ( var i = 0; i < s_c_hakbun.length; i++) 
		{
			if(i>0)
				{
					arr += ",";	
				}
			arr += s_c_hakbun[i].value;
			
		} 
		
		var data={"checkedHakbun":arr,"gwa_code": $('#gwa_code').val()};
		
		$.ajax({
			type:"post",
			data:data,
			url:"deleteStudent.do",
			dataType:"json",
			success:function(data1)
			{
					$("#output").empty();
					var head = "<tr><td></td><td>학번</td><td>성명</td><td>주소</td>"+
							   "<td>전공</td><td>비밀번호</td><td>이메일</td><td>전화</td>"+
							   "<td>주민번호</td><td>성별</td></tr>";
	 					
					$("#output").append(head);
					/* $('#listTable').html(''); */
					
					$(data1).each(function(index,item){
						output+="<tr>";
						output+='<td><input type="button" value="수정" onclick="update(\''+item.hakbun+'\',\''+item.name+'\',\''+item.address+'\',\''+item.passwd+'\',\''+item.email+'\',\''+item.tel+'\',\''+item.jumin1+'\',\''+item.jumin2+'\',\''+item.gender+'\')"></td>';
						output+="<td><input type=checkbox name=s_c_hakbun value="+item.hakbun+">"+item.hakbun+"</td>";
						output+="<td>" + item.name + "</td>";
						output+="<td>" + item.address + "</td>";
						output+="<td>" + item.gwa_code + "</td>";
						output+="<td>" + item.passwd + "</td>";
						output+="<td>" + item.email + "</td>";
						output+="<td>" + item.tel + "</td>";
						output+="<td>" + item.jumin1 +"-"+item.jumin2 + "</td>";
							
						output+="<td>" + item.gender + "</td>";
						output+="</tr>";
						
						$("#output").append(output);
						output = '';
					});
			},
			error:function(e){
			}
		});
	}
	
	function update(obj1,obj2,obj3,obj4,obj5,obj6,obj7,obj8,obj9)
	{
		alert('obj9 :' + obj9);
		if(obj9=='여')
		{
			 $('input:radio[name=gender]:input[value=F]').prop('checked',true);
			
		}else
		{
			 $('input:radio[name=gender]:input[value=M]').prop('checked',true);
		}
		
		save_gubun = 1; // 수정시 학번 중복체크를 하지 않도록 플래그 값을 1로 변경
		oldhakbun = obj1;
		hakbun.value=obj1;
		name1.value=obj2;
		address.value=obj3;
		passwd.value=obj4;
		email.value=obj5;
		tel.value=obj6;
		jumin1.value=obj7;
		jumin2.value=obj8;
	}
	
	function finish()
	{
		location.href='staffMain.do';
	}	
	
</script>
</head> 
<body>

 <div class="command">
 <table class="table2">
 	<tr>
 		<th>
 			<spring:message code="student.form.hakbun"/>
 		</th>
 		<td>
 			<input type="text" id="hakbun" />
 		</td>
 		<th>
 			<spring:message code="student.form.name"/>
 		</th>
 		<td>
 			<input type="text" id="name1" />
 		</td>
 	</tr>
 	<tr>
 		<th>
 			<spring:message code="student.form.address"/>
 		</th>
 		<td>
 			<input type="text" id="address" size="50"/>
 		</td>
 		<th>
 			<spring:message code="student.form.gwa_code"/>
 		</th>
 		<td>
 			${gwaName}<input type="hidden" id="gwa_code" value="${gwa_code }">
 		</td>
 	</tr>
 	<tr>
 		<th>
 			<spring:message code="student.form.passwd"/>
 		</th>
 		<td>
 			<input type="text" id="passwd" />
 		</td>
 		<th>
 			<spring:message code="student.form.email"/>
 		</th>
 		<td>
 			<input type="text" id="email"/>
 		</td>
 	</tr>
 	<tr>
 		<th>
 			<spring:message code="student.form.tel"/>
 		</th>
 		<td>
 			<input type="text" id="tel"/>
 		</td>
 		<th>
 			<spring:message code="student.form.jumin"/>
 		</th>
 		<td>
 			<input type="text" id="jumin1" />-<input type="text" id="jumin2" />
 		</td>
 	</tr>
 	<tr>
 		<th>
 			<spring:message code="student.form.gender"/>
 		</th>
 		<td>
 			<input name="gender" type="radio" value="M" checked='checked'/>남성 	
 		    <input name="gender" type="radio" value="F" />여성
 		</td>
 	</tr>
 </table>
 	
 	<input type="button" id="btnSave" value="저장" onclick="save()" />
	<input type="button" id="btnDel" value="삭제" onclick="del()" /> 
	<input type="button" id="btnExit" value="종료" onclick="finish()" />
	</div>
 
<h2>학과생 목록</h2>
 <div id="stuTable" class="defTable">
 	<table id="output">
 		<tr id="thfix">
 			<th></th>
  			<th>학번</th>
 			<th>성명</th>
 			<th>주소</th>
 			<th>전공</th>
 			<th>비밀번호</th>
 			<th>이메일</th>
 			<th>전화</th>
 			<th>주민번호</th>
 			<th>성별</th>
		</tr>
		<c:forEach var="list" items="${studentList}">
		<tr>
			<td>
				<input type="button" value="수정" 
					onclick="update('${list.hakbun}','${list.name}','${list.address}','${list.passwd}','${list.email}','${list.tel}','${list.jumin1}','${list.jumin2 }','${list.gender}')" />
			</td>
			<td>
				<input type="checkbox" name="s_c_hakbun" value="${list.hakbun }" />${list.hakbun }
			</td>
			<td>
				${list.name }
			</td>
			<td>
				${list.address }
			</td>
			<td>
				${list.gwa_code }
			</td>
			<td>
				${list.passwd }
			</td>
			<td>
				${list.email }
			</td>
			<td>
				${list.tel }
			</td>
			<td>
				${list.jumin1 }-${list.jumin2 }
			</td>
			<td>
				${list.gender }
			</td>
		</tr>
		</c:forEach> 			
 	</table>
 </div>
</body>
</html>
